var contextPath = $("#contextPath").val();
$(function() {
	var start=0,page=1,showNumber=6;
	var goodsList = $("#hotGoods");
	var isload = true;
	dataInit();
	var state = history.state;
	if(state != null){
		var key = state.key;
		if(key != null){
			var oStart = state.start,start = state.start,page = state.page,top = state.top;
			var data = {start:0,showNumber:oStart};
			oHotGoodsList(data,top);
		}else{
			hotGoodsList();
		}
	
	}else{
		hotGoodsList();
	}
	
	
	//banner
	var banner = new Swiper('.banner .swiper-container', {
	  loop: true,
	  autoplay: 3000,
	  autoHeight: true
	});
		
	//推荐
	var mySwiper = new Swiper('.scroll', {
	  freeMode: false,
	  slidesPerView: 'auto',
	  autoHeight: true
	});

function dataInit(){
	
	$.ajax({
		url:"getRecommended",
		type:"post",
		async:true,
		dataType:"json",
		success : function(data, textStatus) {
			var state = data.result.state;
			if(state == 1){
				$.each(data.rHeadList,function(i,v){
					banner.appendSlide('<div class="swiper-slide"><a href="'+ v.hrefUrl +'"><img src="'+ v.picUrl +'" width="100%" alt=""></a></div>');
				});
				
				$.each(data.rMiddleList,function(i,v){
					if(v.is_points_buy == "01"){
						if(v.thumb == ""){
							if(v.change_price == 0){
								mySwiper.appendSlide('<div class="swiper-slide scroll__item"> <a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.objId + '"><span class="commodity__img"><i class="vip_mark"></i><img src="'+contextPath+'/static/images/icon_default_02.jpg"></span><p class="commodity__txt">'+ v.title +'</p></a><div class="commodity__live tj"><span class="dib vm commodity__integral">' + v.change_points + '<em class="n">兑换券</em></span><span class="dib vm joiner"></span><span class="dib vm commodity__live_marketprice" style="display: none;">市价'+ v.price +'</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>');
							}else{
								mySwiper.appendSlide('<div class="swiper-slide scroll__item"> <a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.objId + '"><span class="commodity__img"><i class="vip_mark"></i><img src="'+contextPath+'/static/images/icon_default_02.jpg"></span><p class="commodity__txt">'+ v.title +'</p></a><div class="commodity__live tj"><span class="dib vm commodity__integral">' + v.change_points + '<em class="n">兑换券</em></span><span class="dib vm joiner">+</span><span class="dib vm commodity__live_price">'+ v.change_price + '<em class="n">元</em></span><span class="dib vm commodity__live_marketprice" style="display: none;">市价'+ v.price +'</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>');
							}
						}else{
							if(v.change_price == 0){
								mySwiper.appendSlide('<div class="swiper-slide scroll__item"> <a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.objId + '"><span class="commodity__img"><i class="vip_mark"></i><img src="'+ v.thumb +'" onerror="this.src=\''+contextPath+'/static/images/icon_default_02.jpg\'"></span><p class="commodity__txt">'+ v.title +'</p></a><div class="commodity__live tj"><span class="dib vm commodity__integral">' + v.change_points + '<em class="n">兑换券</em></span><span class="dib vm joiner"></span><span class="dib vm commodity__live_marketprice" style="display: none;">市价'+ v.price +'</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>');
							}else{
								mySwiper.appendSlide('<div class="swiper-slide scroll__item"> <a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.objId + '"><span class="commodity__img"><i class="vip_mark"></i><img src="'+ v.thumb +'" onerror="this.src=\''+contextPath+'/static/images/icon_default_02.jpg\'"></span><p class="commodity__txt">'+ v.title +'</p></a><div class="commodity__live tj"><span class="dib vm commodity__integral">' + v.change_points + '<em class="n">兑换券</em></span><span class="dib vm joiner">+</span><span class="dib vm commodity__live_price">'+ v.change_price + '<em class="n">元</em></span><span class="dib vm commodity__live_marketprice" style="display: none;">市价'+ v.price +'</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>');
							}
						}
					}else{
						if(v.thumb == ""){
							var context='<div class="swiper-slide scroll__item"><a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.objId + '"><span class="commodity__img"><img src="'+contextPath+'/static/images/icon_default_02.jpg">';
							if(v.return_points>0){
								context+='<span class="layer_voucher">赠 <em class="n">'+ v.return_points +'</em>兑换券</span></span><p class="commodity__txt">'+ v.title +'</p></a><div class="commodity__live dt f0"><span class="dib vm commodity__live_price">&yen;'+ v.real_price +'</span><span class="dib vm commodity__live_marketprice">市价'+ v.price +'</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}else{
								context+='</span><p class="commodity__txt">'+ v.title +'</p></a><div class="commodity__live dt f0"><span class="dib vm commodity__live_price">&yen;'+ v.real_price +'</span><span class="dib vm commodity__live_marketprice">市价'+ v.price +'</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}
							mySwiper.appendSlide(context);
						}else{
							var context1='<div class="swiper-slide scroll__item"> <a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.objId + '"><span class="commodity__img"><img src="'+ v.thumb +'" onerror="this.src=\''+contextPath+'/static/images/icon_default_02.jpg\'"> ';
							if(v.return_points>0){
								context1+='<span class="layer_voucher">赠 <em class="n">'+ v.return_points +'</em>兑换券</span></span><p class="commodity__txt">'+ v.title +'</p></a><div class="commodity__live dt f0"> <span class="dib vm commodity__live_price">&yen;'+ v.real_price +'</span><span class="dib vm commodity__live_marketprice" style="display: none;">市价'+ v.price +'</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}else{
								context1+='</span><p class="commodity__txt">'+ v.title +'</p></a><div class="commodity__live dt f0"> <span class="dib vm commodity__live_price">&yen;'+ v.real_price +'</span><span class="dib vm commodity__live_marketprice" style="display: none;">市价'+ v.price +'</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}
							mySwiper.appendSlide(context1);
						}
					}
				});
				
			}
			
			
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
			layer.open({
			    content:textStatus,
			    time: 2
			});
		}
	});		
}
var timesr;
dropload.init(function(){
	
})
dropload.scroll(function(t){
	var top;
	clearTimeout(timesr);
	timesr = setTimeout(function(){
		top = document.querySelector('.content').scrollTop;
		var stateObj = {key:Math.random(),start:start,page:page,top:top};
		window.history.replaceState(stateObj,"","");
	},500); 
	if(t<5&&isload){
		$(".lodingStyle").show();
		isload = false;
		hotGoodsList();
	}

});


$(".search__btn").on("click",function(){
	var search = document.getElementById("search").value;
	search = encodeURIComponent(search);
	location.href=contextPath+'/goods?search='+search;
})

function hotGoodsList(){
	$.ajax({
		url:"getHotGoodsList",
		type:"post",
		async:true,
		data:{start:start,showNumber:showNumber},
		dataType:"json",
		success : function(data, textStatus) {
			$(".lodingStyle").hide();
			var state = data.result.state;
			var html = "";
			if(state == 1){
				$.each(data.rHotGoodsList,function(i,v){
					if(v.is_points_buy == "01"){
						if(v.thumb == ""){
							if(v.change_price == 0){
								html += '<li class="popular__item"><a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.id + '"><span class="commodity__img"><i class="vip_mark"></i><img src="'+contextPath+'/static/images/icon_default_02.jpg"></span><p class="commodity__txt">' + v.title + '</p></a><div class="commodity__live tj"><span class="dib vm commodity__integral">' + v.change_points + '<em class="n">兑换券</em></span><span class="dib vm joiner"></span><span class="dib vm commodity__live_marketprice" style="display:none">市价1820</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}else{
								html += '<li class="popular__item"><a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.id + '"><span class="commodity__img"><i class="vip_mark"></i><img src="'+contextPath+'/static/images/icon_default_02.jpg"></span><p class="commodity__txt">' + v.title + '</p></a><div class="commodity__live tj"><span class="dib vm commodity__integral">' + v.change_points + '<em class="n">兑换券</em></span><span class="dib vm joiner">+</span><span class="dib vm commodity__live_price">'+ v.change_price + '<em class="n">元</em></span><span class="dib vm commodity__live_marketprice" style="display:none">市价1820</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}
						}else{
							if(v.change_price == 0){
								html += '<li class="popular__item"><a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.id + '"><span class="commodity__img"><i class="vip_mark"></i><img src="'+ v.thumb +'" width="100%" alt="" onerror="'+contextPath+'/static/images/icon_default_02.jpg"></span><p class="commodity__txt">' + v.title + '</p></a><div class="commodity__live tj"><span class="dib vm commodity__integral">' + v.change_points + '<em class="n">兑换券</em></span><span class="dib vm joiner"></span><span class="dib vm commodity__live_marketprice" style="display:none">市价1820</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}else{
								html += '<li class="popular__item"><a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.id + '"><span class="commodity__img"><i class="vip_mark"></i><img src="'+ v.thumb +'" width="100%" alt="" onerror="'+contextPath+'/static/images/icon_default_02.jpg"></span><p class="commodity__txt">' + v.title + '</p></a><div class="commodity__live tj"><span class="dib vm commodity__integral">' + v.change_points + '<em class="n">兑换券</em></span><span class="dib vm joiner">+</span><span class="dib vm commodity__live_price">'+ v.change_price + '<em class="n">元</em></span><span class="dib vm commodity__live_marketprice" style="display:none">市价1820</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}
						}
					}else{
						if(v.thumb == ""){
							html+='<li class="popular__item"><a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.id + '"><span class="commodity__img"><img src="'+contextPath+'/static/images/icon_default_02.jpg">';
							if(v.return_points>0){
								html+='<span class="layer_voucher">赠 <em class="n">'+v.return_points+'</em>兑换券</span></span><p class="commodity__txt">' + v.title + '</p></a><div class="commodity__live tj"><span class="dib vm commodity__live_price">&yen;'+ v.real_price + '</span><span class="dib vm commodity__live_marketprice">市价'+ v.price + '</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}else{
								html+='</span><p class="commodity__txt">' + v.title + '</p></a><div class="commodity__live tj"><span class="dib vm commodity__live_price">&yen;'+ v.real_price + '</span><span class="dib vm commodity__live_marketprice">市价'+ v.price + '</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}
						}else{
							html += '<li class="popular__item"><a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.id + '"><span class="commodity__img"><img src="'+ v.thumb +'" width="100%" alt="" onerror="'+contextPath+'/static/images/icon_default_02.jpg">';
							if(v.return_points>0){
								html+='<span class="layer_voucher">赠 <em class="n">'+v.return_points+'</em>兑换券</span></span><p class="commodity__txt">' + v.title + '</p></a><div class="commodity__live tj"><span class="dib vm commodity__live_price">&yen;'+ v.real_price + '</span><span class="dib vm commodity__live_marketprice">市价'+ v.price + '</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}else{
								html+='</span><p class="commodity__txt">' + v.title + '</p></a><div class="commodity__live tj"><span class="dib vm commodity__live_price">&yen;'+ v.real_price + '</span><span class="dib vm commodity__live_marketprice">市价'+ v.price + '</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							} 
						}
					}
				});
				$("#foo").before(html);
				start=page*showNumber;
				page++;
				if(data.rHotGoodsList.length == showNumber){
					isload = true;	
				}
				var top = document.body.scrollTop;
				var stateObj={key:Math.random(),start:start,page:page,top:top};
				window.history.replaceState(stateObj,"","");
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
			layer.open({
			    content:textStatus,
			    time: 2
			});
		}
	});	
}



function oHotGoodsList(data,top){
	var contentEl = document.querySelector('.content')
	$.ajax({
		url:"getHotGoodsList",
		type:"post",
		async:true,
		data:data,
		dataType:"json",
		success : function(data, textStatus) {
			$(".lodingStyle").hide();
			var state = data.result.state;
			var html = "";
			if(state == 1){
				$.each(data.rHotGoodsList,function(i,v){
					if(v.is_points_buy == "01"){
						if(v.thumb == ""){
							if(v.change_price == 0){
								html += '<li class="popular__item"><a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.id + '"><span class="commodity__img"><i class="vip_mark"></i><img src="'+contextPath+'/static/images/icon_default_02.jpg"></span><p class="commodity__txt">' + v.title + '</p></a><div class="commodity__live tj"><span class="dib vm commodity__integral">' + v.change_points + '<em class="n">兑换券</em></span><span class="dib vm joiner"></span><span class="dib vm commodity__live_marketprice" style="display:none">市价1820</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}else{
								html += '<li class="popular__item"><a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.id + '"><span class="commodity__img"><i class="vip_mark"></i><img src="'+contextPath+'/static/images/icon_default_02.jpg"></span><p class="commodity__txt">' + v.title + '</p></a><div class="commodity__live tj"><span class="dib vm commodity__integral">' + v.change_points + '<em class="n">兑换券</em></span><span class="dib vm joiner">+</span><span class="dib vm commodity__live_price">'+ v.change_price + '<em class="n">元</em></span><span class="dib vm commodity__live_marketprice" style="display:none">市价1820</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}
						}else{
							if(v.change_price == 0){
								html += '<li class="popular__item"><a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.id + '"><span class="commodity__img"><i class="vip_mark"></i><img src="'+ v.thumb +'" width="100%" alt="" onerror="'+contextPath+'/static/images/icon_default_02.jpg"></span><p class="commodity__txt">' + v.title + '</p></a><div class="commodity__live tj"><span class="dib vm commodity__integral">' + v.change_points + '<em class="n">兑换券</em></span><span class="dib vm joiner"></span><span class="dib vm commodity__live_marketprice" style="display:none">市价1820</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}else{
								html += '<li class="popular__item"><a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.id + '"><span class="commodity__img"><i class="vip_mark"></i><img src="'+ v.thumb +'" width="100%" alt="" onerror="'+contextPath+'/static/images/icon_default_02.jpg"></span><p class="commodity__txt">' + v.title + '</p></a><div class="commodity__live tj"><span class="dib vm commodity__integral">' + v.change_points + '<em class="n">兑换券</em></span><span class="dib vm joiner">+</span><span class="dib vm commodity__live_price">'+ v.change_price + '<em class="n">元</em></span><span class="dib vm commodity__live_marketprice" style="display:none">市价1820</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}
						}
					}else{
						if(v.thumb == ""){
							html+='<li class="popular__item"><a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.id + '"><span class="commodity__img"><img src="'+contextPath+'/static/images/icon_default_02.jpg">';
							if(v.return_points>0){
								html+='<span class="layer_voucher">赠 <em class="n">'+v.return_points+'</em>兑换券</span></span><p class="commodity__txt">' + v.title + '</p></a><div class="commodity__live tj"><span class="dib vm commodity__live_price">&yen;'+ v.real_price + '</span><span class="dib vm commodity__live_marketprice">市价'+ v.price + '</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}else{
								html+='</span><p class="commodity__txt">' + v.title + '</p></a><div class="commodity__live tj"><span class="dib vm commodity__live_price">&yen;'+ v.real_price + '</span><span class="dib vm commodity__live_marketprice">市价'+ v.price + '</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}
						}else{
							html += '<li class="popular__item"><a href="'+ contextPath +'/goods/detailPage?goodsId=' + v.id + '"><span class="commodity__img"><img src="'+ v.thumb +'" width="100%" alt="" onerror="'+contextPath+'/static/images/icon_default_02.jpg">';

							if(v.return_points>0){
								html+='<span class="layer_voucher">赠 <em class="n">'+v.return_points+'</em>兑换券</span></span><p class="commodity__txt">' + v.title + '</p></a><div class="commodity__live tj"><span class="dib vm commodity__live_price">&yen;'+ v.real_price + '</span><span class="dib vm commodity__live_marketprice">市价'+ v.price + '</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}else{
								html+='</span><p class="commodity__txt">' + v.title + '</p></a><div class="commodity__live tj"><span class="dib vm commodity__live_price">&yen;'+ v.real_price + '</span><span class="dib vm commodity__live_marketprice">市价'+ v.price + '</span><span class="dib vm commodity__live_buy icon_car add_shopping_cart_btn"></span></div></div>';
							}
						}
					}
				});
				$("#foo").before(html);
				contentEl.scrollTop = top;

				
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
			layer.open({
			    content:textStatus,
			    time: 2
			});
		}
	});	
}

});




function searchTop(scrollTop){
    var bnHt = 320
    var el = document.querySelector('.search')
    var icon = el.querySelector('.icon__search')
    var input = el.querySelector('.search__input')
    var btn  = el.querySelector('.search__btn')
    
    var	ht = el.offsetHeight
    var scale = scrollTop/bnHt

    scale = scale > 1 ? 1 : scale

    if(scrollTop >= ht){
      icon.classList.add('type_01')
      input.style.cssText="background:#e6e6e6;color:#666;"
      btn.style.color = "#999"
    }else{
      input.removeAttribute('style')
      btn.removeAttribute('style')
      icon.classList.remove('type_01')
    }
    if(bnHt < scrollTop){
      el.style.boxShadow = "rgba(51, 51, 51, .3) 0 2px 4px 2px"
    }else{
      el.style.boxShadow = "none"
    }
    el.style.background = "rgba(255,255,255," + scale + ")"

  }

window.addEventListener('DOMContentLoaded',function(){
	var content = document.querySelector('.content')
    var scrollTop = content.scrollTop
   
    // 默认触发
    searchTop(scrollTop)
    // resize触发
    window.addEventListener('resize',function(){
    	searchTop(scrollTop)
    },false)        
    // scroll触发
    content.addEventListener('scroll',function(){
    	scrollTop = content.scrollTop
    	
    	searchTop(scrollTop)
    },false)
  },false)